<?php


namespace services;

use services\upload\Upload;

/**
 * Class UploadService
 * @package services
 */
class UploadService
{

    /**
     * @var array
     */
    protected static $upload = [];

    /**
     * @param $type
     *
     * @return Upload
     */
    public static function init($type = null)
    {
        if (is_null($type)) {
            $type = (int)sysconfig('upload_type', 1);
        }
        if (isset(self::$upload['upload_' . $type])) {
            return self::$upload['upload_' . $type];
        }
        $type   = (int)$type;
        $config = [];
        switch ($type) {
            case 2://七牛
                $config = [
                    'accessKey'     => sysconfig('qiniu_accessKey'),
                    'secretKey'     => sysconfig('qiniu_secretKey'),
                    'uploadUrl'     => sysconfig('qiniu_uploadUrl'),
                    'storageName'   => sysconfig('qiniu_storage_name'),
                    'storageRegion' => sysconfig('qiniu_storage_region'),
                ];
                break;
            case 3:// oss 阿里云
                $config = [
                    'accessKey'     => sysconfig('oss_accessKey'),
                    'secretKey'     => sysconfig('oss_secretKey'),
                    'uploadUrl'     => sysconfig('oss_uploadUrl'),
                    'storageName'   => sysconfig('oss_storage_name'),
                    'storageRegion' => sysconfig('oss_storage_region'),
                ];
                break;
            case 4:// cos 腾讯云
                $config = [
                    'accessKey'     => sysconfig('cos_accessKey'),
                    'secretKey'     => sysconfig('cos_secretKey'),
                    'uploadUrl'     => sysconfig('cos_uploadUrl'),
                    'storageName'   => sysconfig('cos_storage_name'),
                    'storageRegion' => sysconfig('cos_storage_region'),
                ];
                break;
        }
        return self::$upload['upload_' . $type] = new Upload($type, $config);
    }

}
